package com.gk.panda.apis.controller;

import com.alibaba.nacos.common.utils.HttpMethod;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.panda.apis.entity.deviceHandheldTerminal.dto.HandheldTerminalDto;
import com.gk.panda.apis.entity.deviceHandheldTerminal.dto.HandheldTerminalListDto;
import com.gk.panda.apis.entity.deviceHandheldTerminal.vo.HandheldTerminalListVo;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.DeviceHandheldTerminalService;
import com.gk.panda.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * @ClassName: DeviceHandheldTerminalController
 * @Description: 手持终端接口
 * @author: Sir.yang
 * @date: 2022/11/4 15:50
 * @version: 1.0
 **/
@RestController
@Api(tags = "手持终端设备接口")
@RequestMapping("/edmp/handheldTerminal")
public class DeviceHandheldTerminalController {

    @Autowired
    private DeviceHandheldTerminalService deviceHandheldTerminalService;

    @ApiOperation("获取手持终端设备列表")
    @PostMapping("/list")
    public Result<Page<HandheldTerminalListVo>> getHandheldTerminalList(@RequestBody HandheldTerminalListDto dto) {
        return Result.ok(deviceHandheldTerminalService.getHandheldTerminalList(dto));
    }

    @ApiOperation("修改手持终端设备信息")
    @Log
    @PostMapping("/change")
    public Result<Integer> changeHandheldTerminalInfo(@RequestBody HandheldTerminalDto dto) throws UnsupportedEncodingException {
        return Result.ok(deviceHandheldTerminalService.changeHandheldTerminalInfo(dto));
    }

    @ApiOperation("新增手持终端设备信息")
    @Log
    @PostMapping("/save")
    public Result<Integer> saveHandheldTerminalInfo(@RequestBody HandheldTerminalDto dto) throws UnsupportedEncodingException {
        return Result.ok(deviceHandheldTerminalService.saveHandheldTerminalInfo(dto));
    }

    @ApiOperation("删除设备")
    @Log
    @GetMapping("/remove/{deviceId}")
    public Result<Integer> removeHandheldTerminalInfo(@PathVariable Integer deviceId) throws UnsupportedEncodingException {
        return Result.ok(deviceHandheldTerminalService.removeHandheldTerminalInfo(deviceId));
    }

    @ApiOperation(value = "下载手持终端导入模板", httpMethod = HttpMethod.GET)
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) {
        deviceHandheldTerminalService.downloadTemplate(response);
    }

    @ApiOperation(value = "批量导入手持终端信息", httpMethod = HttpMethod.POST)
    @Log
    @PostMapping("/import")
    public Result<String> importTerminal(@RequestPart("file") MultipartFile file) throws UnsupportedEncodingException {
        String result =  deviceHandheldTerminalService.importTerminal(file);
        if (result.equals("success")) {
            return Result.write(200,"导入成功");
        } else if (result.equals("failed")) {
            return Result.write(201,"导入失败");
        } else {
            return Result.write(201,result);
        }
    }

    @ApiOperation(value = "批量导出手持终端信息", httpMethod = HttpMethod.GET)
    @Log
    @GetMapping("/export")
    public void exportTerminal(HttpServletResponse response) {
        deviceHandheldTerminalService.exportTerminal(response);
    }
}
